//
// Created by Administrator on 2024/3/11.
//

#include<bits/stdc++.h>

using namespace std;
using ll = long long;
#define endl '\n'

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, q;
    int op, t, x;
    cin >> n >> q;
    vector<queue<int>> Q(n);
    while (q--) {
        cin >> op;
        if (op == 0) {
            cin >> t >> x;
            Q[t].push(x);
        } else if (op == 1) {
            cin >> t;
            if (Q[t].empty())continue;
            cout << Q[t].front() << endl;
        } else {
            cin >> t;
            if (Q[t].empty())continue;
            Q[t].pop();
        }
    }

    return 0;
}
